//
//  UIScrollView+YYRefresh.m
//

#import "UIScrollView+YYRefresh.h"


@implementation UIScrollView (YYRefresh)

/** 添加头部刷新 */
- (void)addHeaderRefresh:(MJRefreshComponentAction)block{
    self.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:block];
}
/** 添加脚部自动刷新 */
- (void)addAutoFooterRefresh:(MJRefreshComponentAction)block{
    self.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:block];
    MJRefreshBackGifFooter *footer = (MJRefreshBackGifFooter *)self.mj_footer;
    footer.stateLabel.textColor = kCOLOR_HEX(@"#999999");
}

/** 添加脚步返回刷新 */
- (void)addBackFooterRefresh:(MJRefreshComponentAction)block{
    self.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:block];
}
/** 结束头部刷新 */
- (void)endHeaderRefresh{
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        [self.mj_header endRefreshing];
    }];
}
/** 结束脚部刷新 */
- (void)endFooterRefresh{
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        [self.mj_footer endRefreshing];
    }];
}
/** 开始头部刷新 */
- (void)beginHeaderRefresh{
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        [self.mj_header beginRefreshing];
    }];
}
/** 开始脚部刷新 */
- (void)beginFooterRefresh{
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        MJRefreshBackGifFooter *footer = (MJRefreshBackGifFooter *)self.mj_footer;
//        [footer setTitle: @"正在加载数据…" forState: MJRefreshStateRefreshing];
        [self.mj_footer beginRefreshing];
    }];
}

/** 开始脚部刷新数据 */
- (void)beginFooterRefreshData{
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        MJRefreshBackGifFooter *footer = (MJRefreshBackGifFooter *)self.mj_footer;
        [footer setTintColor: kCOLOR_HEX(@"#999999")];
        footer.stateLabel.textColor = kCOLOR_HEX(@"#999999");
//        [footer setTitle: @"加载中…" forState: MJRefreshStateRefreshing];
        [self.mj_footer beginRefreshing];
    }];
}

/** 结束脚步刷新并设置没有数据 */
- (void)endFooterRefreshWithNoData{
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        MJRefreshBackGifFooter *footer = (MJRefreshBackGifFooter *)self.mj_footer;
//        [footer setTitle: @"暂无搜索结果" forState: MJRefreshStateNoMoreData];
        [self.mj_footer endRefreshingWithNoMoreData];
    }];
}

/** 结束脚步刷新并设置没有更多数据 */
- (void)endFooterRefreshWithNoMoreData{
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        MJRefreshBackGifFooter *footer = (MJRefreshBackGifFooter *)self.mj_footer;
//        [footer setTitle: @"no more data" forState: MJRefreshStateNoMoreData];
        [self.mj_footer endRefreshingWithNoMoreData];
    }];
}

/** 结束脚步刷新-自定义图标，并设置没有数据 */
- (void)endFooterRefreshCustomWithNoData{
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        MJRefreshBackGifFooter *footer = (MJRefreshBackGifFooter *)self.mj_footer;
//        [footer setTitle: @"暂无搜索结果" forState: MJRefreshStateNoMoreData];
        [self.mj_footer endRefreshingWithNoMoreData];
    }];
}

/** 结束脚步刷新-自定义图标，并设置没有更多数据 */
- (void)endFooterRefreshCustomWithNoMoreData{
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        MJRefreshBackGifFooter *footer = (MJRefreshBackGifFooter *)self.mj_footer;
//        [footer setTitle: @"no more data" forState: MJRefreshStateNoMoreData];
        [self.mj_footer endRefreshingWithNoMoreData];
    }];
}

/** 重置没有更多的数据（消除没有更多数据的状态） */
-(void)endFooterResetNoMoreData{
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        [self.mj_footer resetNoMoreData];
    }];
}

/** 结束脚步刷新,  并设置没有更多数据 或 重置没有更多的数据（消除没有更多数据的状态）,  适用于（TABTableAnimated）  */
- (void)tab_endFooterRefreshWithNoMoreFlag:(BOOL)noMoreFlag {
    
    if (self.mj_footer == nil) {
        self.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            
        }];
        MJRefreshBackGifFooter *footer = (MJRefreshBackGifFooter *)self.mj_footer;
        footer.stateLabel.textColor = kCOLOR_HEX(@"#999999");
    }

    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        
        MJRefreshBackGifFooter *footer = (MJRefreshBackGifFooter *)self.mj_footer;
        if (noMoreFlag) {
            footer.hidden = NO;
//            [footer setTitle: @"没有更多了" forState: MJRefreshStateNoMoreData];
            [self.mj_footer endRefreshingWithNoMoreData];
        }else {
            [self.mj_footer resetNoMoreData];
            footer.hidden = YES;
        }
    }];
}

/** 隐藏或显示 头部刷新 */
- (void)headerRefreshAtHidden:(BOOL)isHidden{
    self.mj_header.hidden = isHidden;
}

/** 隐藏或显示 脚步刷新 */
- (void)footerRefreshAtHidden:(BOOL)isHidden{
    self.mj_footer.hidden = isHidden;
}

/** 隐藏或显示 头部刷新和脚步刷新 */
- (void)headerAndFooterRefreshAtHidden:(BOOL)isHidden{
    [self headerRefreshAtHidden: isHidden];
    [self footerRefreshAtHidden: isHidden];
}

@end
